package com.spring.dao.blog.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.spring.exception.DaoException;
import com.spring.bean.blog.BlogComment;
import com.spring.dao.blog.BlogCommentDao;
import com.spring.bean.page.Page;
import com.spring.bean.page.PageView;
import com.spring.dao.DaoSupport;

@Repository
public class BlogCommentDaoImpl extends DaoSupport implements BlogCommentDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	private static final String TABLE_BLOG_COMMENT = "blog_comment";

	/** insert */
	@Override
	public boolean insert(BlogComment blogComment) throws DaoException{
		try{
			return insert(jdbcTemplate, blogComment);
		} catch (Exception e) {
			throw new DaoException("BlogComment --- insert error : " + e.getMessage());
		}
	}

	/** udpate */
	@Override
	public boolean update(BlogComment blogComment) throws DaoException{
		try{
			return update(jdbcTemplate, blogComment);
		} catch (Exception e) {
			throw new DaoException("BlogComment --- update error : " + e.getMessage());
		}
	}

	/** delete */
	@Override
	public boolean delete(Integer id) throws DaoException{
		try{
			return deleteById(jdbcTemplate, TABLE_BLOG_COMMENT, id);
		} catch (Exception e) {
			throw new DaoException("BlogComment --- delete error : " + e.getMessage());
		}
	}

	/** getEntityById */
	@Override
	public BlogComment getById(Integer id) throws DaoException{
		try{
			return getById(jdbcTemplate, BlogComment.class , id);
		} catch (Exception e) {
			throw new DaoException("BlogComment --- query by id error : " + e.getMessage());
		}
	}

	/** queryForList */
	@Override
	public List<BlogComment> queryForList() throws DaoException{
		String sql = "select id,blog_id,pid,pb_id,content,comm_time from " + TABLE_BLOG_COMMENT;
		try{
			return queryForList(jdbcTemplate, BlogComment.class, sql);
		} catch (Exception e) {
			throw new DaoException("BlogComment --- query list error : " + e.getMessage());
		}
	}

	/** queryForPage */
	@Override
	public PageView<BlogComment> queryForPage(Page page) throws DaoException{
		String sql = "select SQL_CALC_FOUND_ROWS id,blog_id,pid,pb_id,content,comm_time from " + TABLE_BLOG_COMMENT;
		try{
			return queryForPage(jdbcTemplate, BlogComment.class, sql, page);
		} catch (Exception e) {
			throw new DaoException("BlogComment --- query page error : " + e.getMessage());
		}
	}
}